﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Net.Mail;
using System.Text;
using System.Web;


namespace YogaStudio.WebUI.Helper
{
    public class EmailSender
    {
        //private ApplicationDbContext db = new ApplicationDbContext();

        
        public bool PrepareMail(string mailTo, string userName, StringBuilder body)
        {
            string MailFrom = ConfigurationManager.AppSettings.Get("MailFrom");

            MailMessage msg = new MailMessage();
            msg.From = new MailAddress(MailFrom, "YogaStudio.com");
            msg.To.Add(new MailAddress(mailTo, userName));
          
            msg.Subject = string.Format("YogaStudio: {0}", "Welcome to YogaStudio");



            string siteurl = "http://localhost:49821/Account/Confirm?userName=";
            //string smsg = "<>New Registration on our website, find your details below:<br>";
            
            string smsg= "<br><b>Your account is not activated still, please activate it now: </b>";
            smsg += "<br><b>Title of Post:" + "<a href=" + siteurl + userName + "> Click Here</a>";
            smsg += "<br><br><br><br>";

       // http://localhost:49821/Account/Confirm/?userName=user1

            body.Append(smsg);
            msg.Body = BuildBody(userName, body);
            msg.IsBodyHtml = true;

            SendEmail(msg);
            return true;

        }

        public bool ForgotPasswordEmail(string mailTo, string userName, StringBuilder body)
        {
            string MailFrom = ConfigurationManager.AppSettings.Get("MailFrom");

            MailMessage msg = new MailMessage();
            msg.From = new MailAddress(MailFrom, "YogaStudio.com");
            msg.To.Add(new MailAddress(mailTo, userName));

            msg.Subject = string.Format("YogaStudio: {0}", "YogaStudio - Reset Email");

            msg.Body = BuildBody(userName, body);
            msg.IsBodyHtml = true;

            SendEmail(msg);
            return true;
        }


        private String BuildBody(String NameTo, StringBuilder body)
        {

            StringBuilder Str = new StringBuilder();
            Str.Append("<span style=\"color: #009871;font-size:12px; font-weight: normal;\"><strong><em> Dear " + NameTo + ",</em></strong>" + "<br/>");
            Str.Append(body);
            Str.Append("<br/>Thank you," + "<br/>");
            Str.Append("Automatic Notification.<br/> Yoga Studio, Work Nazis" + "<br/></span>");
            return Str.ToString();
        }
        public void SendEmail(MailMessage msg)
        {
            try
            {
                string Networked_Password = ConfigurationManager.AppSettings.Get("Networked_Password");
                string Networked_Username = ConfigurationManager.AppSettings.Get("Networked_Username");
                string Host = ConfigurationManager.AppSettings.Get("Host");

                SmtpClient smtpClient = new SmtpClient();

                smtpClient.Host = Host;
                smtpClient.EnableSsl = true;
                System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();
                NetworkCred.UserName = Networked_Username;
                NetworkCred.Password = Networked_Password;
                smtpClient.UseDefaultCredentials = true;
                smtpClient.Credentials = NetworkCred;
                smtpClient.Port = 25;

                smtpClient.Send(msg);
            }
            catch(Exception e)
            {
               

            }

        }
    }
}